package com.mall.permission.service.impl;

import com.mall.permission.model.RolePermission;
import com.mall.permission.mapper.RolePermissionMapper;
import com.mall.permission.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * URL权限关系对应 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-10-20
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addRolePermission(RolePermission rolepermission){
        return super.save(rolepermission);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateRolePermission(RolePermission rolepermission){
        return super.updateById(rolepermission);
    }

    @Override
    public List<RolePermission> selectList(RolePermission rolepermission){
        return super.list(getQueryWrapper(rolepermission));
    }

    @Override
    public Page<RolePermission> selectPage(Page<RolePermission> page, RolePermission rolepermission){
        return super.page(page, getQueryWrapper(rolepermission));
    }

    @Override
    public List<Map<String, String>> allRolePermissions() {
        return baseMapper.allRolePermissions();
    }

    public LambdaQueryWrapper<RolePermission> getQueryWrapper(RolePermission rolepermission){
        return Wrappers.<RolePermission>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(rolepermission.getRid()), RolePermission::getRid, rolepermission.getRid())
            .eq(ObjectUtils.isNotEmpty(rolepermission.getPid()), RolePermission::getPid, rolepermission.getPid())
        ;
    }
}
